package com.woniuxy.springbootrabbitconsumer.comsumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Student;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @Author: linKe
 * @Date: 2022/1/12 11:00
 * @FileName: MyConsumer
 * @version: 1.0
 * @Description: 定义一个bean，消费者，监听对垒，定义处理消息的方法
 * 只要开启就会一直监听
 */
@Log4j2
@Component
public class MyConsumer {
    /**
     * 指定当前方法监听那个队列
     * 常见的三个参数
     * @param msg     消息体
     * @param message 消息对象
     * @param channel 信道
     */
    @RabbitListener(queues = "queueEmailTopic")
    public void receiveEmail(Student msg, Message message, Channel channel) throws IOException {
        Date date = new Date();
        log.info("10s后再处理，关闭订单可支付状态,当前时间为:{}",date);
        log.info("处理email:");
        log.info("msg:{}",msg);
        log.info("message:{}",message);
        log.info("channel:{}",channel);
        log.info("email:{}",message.getMessageProperties().getHeaders().get("email"));
        log.info("email2:{}",message.getMessageProperties().getHeaders().get("email2"));
        // 增加限流方法
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("============================================================================");
    }
    /**
     * 指定当前方法监听那个队列
     * 常见的三个参数
     * @param msg     消息体
     * @param message 消息对象
     * @param channel 信道
     */
    @RabbitListener(queues = "queueSmsTopic")
    public void receiveSms(String msg, Message message, Channel channel) throws IOException {
        Date date = new Date();
        log.info("10s后再处理，关闭订单可支付状态,当前时间为:{}",date);
        log.info("处理sms:");
        log.info("msg:{}",msg);
        log.info("message:{}",message);
        log.info("channel:{}",channel);
        //
        /// channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        log.info("============================================================================");
    }




}
